<?php
class Customers extends MX_Controller
{
    function __construct()
    {
        parent::__construct();        
        $this->load->library('pagination');        
    }
    
    function index($keyword='~',$row=0)
    {   

        $customer = new Customer();
        
        $data['key_word'] = urldecode($keyword);
        if($this->input->post('txtKeyWord'))
        {
            $data['key_word'] = $this->input->post('txtKeyWord');
        }
        
        //paging
        include('paging.php');        		                                         
        // compute total record     
        if($data['key_word'] != '~')
        {
            $customer->like('name', $data['key_word']);
        }        
        $config['base_url']= base_url()."/administrator/customers/index/".$data['key_word'].'/';
        $config['total_rows'] = $customer->count();        
        $config['cur_page'] = $row;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	
        
        $customer = new Customer();
        $customer->limit($config['per_page'], $row)
            ->order_by('id','DESC');                        
        if($data['key_word'] != '~')
        {
            $customer->like('name', $data['key_word']);
        }
                      
        $data['list_customer'] = $customer->get();                        
        $data['view'] = 'customers_index';
        $this->load->view('back_end/template_noright',$data);

    }
    
    function add()
    {
        if( $this->input->post('name'))
        {
            $name = $this->input->post('name');
            $address = $this->input->post('address');
            $email = $this->input->post('email');
            $phone = $this->input->post('phone');
            $fax = $this->input->post('fax');
            $credit_card = $this->input->post('credit_card');        
            $bank_name = $this->input->post('bank_name');         
            $status = $this->input->post('status');             
                
            $customer = new Customer();
            $customer->name = $name;
            $customer->address =$address;            
            $customer->email = $email;
            $customer->phone = $phone;
            $customer->date = date('Y-m-d H:i:s');            
            $customer->fax = $fax;
            $customer->credit_card = $credit_card;
            $customer->bank_name = $bank_name;
            $customer->status = $status;
            
            $customer->save(); 
            redirect('administrator/customers');        
        } else
        {
            $data['view'] = 'customers_add';
            $this->load->view('back_end/template_noright',$data);
        }
        
    }
    function delete()
    {    
        $id = $this->input->post('param');        
        $customer = new Customer();
        $customer->get_by_id($id);                       
        $customer->delete();
            
    }		     
    function edit($id=0,$keyword='~',$row=0)
    {
        if( $this->input->post('name'))
        {            
            $name = $this->input->post('name');
            $address = $this->input->post('address');
            $email = $this->input->post('email');
            $phone = $this->input->post('phone');
            $fax = $this->input->post('fax');
            $credit_card = $this->input->post('credit_card');        
            $bank_name = $this->input->post('bank_name');         
            $status = $this->input->post('status');             
                
            $customer = new Customer();
            $customer->get_by_id($id);
            $customer->name = $name;
            $customer->address =$address;            
            $customer->email = $email;
            $customer->phone = $phone;
            $customer->date = date('Y-m-d H:i:s');            
            $customer->fax = $fax;
            $customer->credit_card = $credit_card;
            $customer->bank_name = $bank_name;
            $customer->status = $status;
            
            $customer->save(); 
            redirect('administrator/customers');        
        } else
        {
            $customer = new Customer();              
            //paging
            $data['key_word'] = urldecode($keyword);
            if($this->input->post('txtKeyWord'))
            {
                $data['key_word'] = $this->input->post('txtKeyWord');
            }
            if($data['key_word'] != '~')
            {
                $customer->like('name', $data['key_word']);
            }  
            include('paging.php');        		                                         
            // compute total record                    
            $config['base_url']= base_url()."/administrator/customers/edit/".$id.'/'.$data['key_word'].'/';
            $config['total_rows'] = $customer->count();        
            $config['cur_page'] = $row;		
            $this->pagination->initialize($config);
            $data['list_link'] = $this->pagination->create_links();	
            
            $customer = new Customer();
            $customer->limit($config['per_page'], $row)
                ->order_by('id','DESC');  
            if($data['key_word'] != '~')
            {
                $customer->like('name', $data['key_word']);
            }                                          
            $data['list_customer'] = $customer->get();  
                
            $customer = new Customer();        
            $data['customer'] = $customer->get_by_id($id);                         
            $data['view'] = 'customers_edit';
            $this->load->view('back_end/template_noright',$data);
        }
    }
    
}
?>